home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / CD CHIP.ISO / WebServ / folkweb / REG.CP_ < prev    next >
Encoding:
Text File  |  1995-08-25  |  5.7 KB  |  242 lines

  1. // reg.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "reg.h"
  6. #include "mainfrm.h"
  7. #include "regview.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CRegApp
  16.  
  17. BEGIN_MESSAGE_MAP(CRegApp, CWinApp)
  18.     //{{AFX_MSG_MAP(CRegApp)
  19.     //}}AFX_MSG_MAP
  20.     // Standard file based document commands
  21.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  22.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  23. END_MESSAGE_MAP()
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CRegApp construction
  27.  
  28. CRegApp::CRegApp()
  29. {
  30.     m_pView = NULL;
  31.     m_pFrame = NULL;
  32. }
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // The one and only CRegApp object
  36.  
  37. CRegApp theApp;
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CRegApp initialization
  41.  
  42. BOOL CRegApp::InitInstance()
  43. {
  44.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  45.  
  46.     m_pFrame = new CMainFrame;
  47.     m_pView = new CRegView;
  48.  
  49.     CCreateContext newContext;
  50.     newContext.m_pNewViewClass = NULL;
  51.     newContext.m_pNewDocTemplate = NULL;
  52.     newContext.m_pLastView = NULL;
  53.     newContext.m_pCurrentFrame = NULL;
  54.     newContext.m_pCurrentDoc = NULL;
  55.  
  56.     DWORD dwStyle = WS_OVERLAPPED;
  57.  
  58.     // create the frame with 0 dimensions since this is background process
  59.     if (!m_pFrame->Create(NULL, NULL, dwStyle, CRect(0,0,0,0),
  60.         NULL, NULL, 0L, &newContext))
  61.     {
  62.         return FALSE;   // will self destruct on failure normally
  63.     }
  64.  
  65.     // assingn main app window
  66.      m_pMainWnd = m_pFrame;
  67.  
  68.     // create the view with 0 dimensions
  69.     m_pView->Create(NULL,NULL,WS_CHILD,CRect(0,0,0,0),m_pFrame,
  70.         AFX_IDW_PANE_FIRST,&newContext);
  71.     m_pView->OnInitialUpdate();
  72.  
  73.     // if we got all file names from WWW Server
  74.     if (m_lpCmdLine[0] != '\0')
  75.     {
  76.         CString sIniFile;
  77.         CString sContentFile;
  78.         CString sOutputFile;
  79.         // extracting file names form command line
  80.         ParseCmdLine(sIniFile, sContentFile, sOutputFile);
  81.         // parsing [Form Literal] Section in CGI ini file created by WWW Server
  82.         ParseIniFile(sIniFile.GetBuffer(0));
  83.         // creating appropriate response
  84.         CreateResponse(sOutputFile, sIniFile);
  85.     }
  86.     // let get out 
  87.     PostQuitMessage(0);
  88.     return TRUE;
  89. }
  90.  
  91. int CRegApp::ExitInstance()
  92. {
  93.     if (m_pView)
  94.         delete m_pView;
  95.     if (m_pFrame)
  96.         delete m_pFrame;
  97.     return 0;
  98. }
  99.  
  100. void CRegApp::ParseCmdLine(CString& sIniFile, CString& sContentFile, CString& sOutputFile)
  101. {
  102.     CString sCmdLine = m_lpCmdLine;
  103.     int nCmdPos = 0;
  104.     int nWordPos = 0;
  105.  
  106.     // skip LWS
  107.     while (nCmdPos < sCmdLine.GetLength() &&
  108.         (sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
  109.         nCmdPos++;
  110.  
  111.     nWordPos = nCmdPos;
  112.  
  113.     // extract ini file path
  114.     while (nCmdPos < sCmdLine.GetLength() &&
  115.         sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
  116.         nCmdPos++;
  117.  
  118.     sIniFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));
  119.  
  120.     // skip LWS
  121.     while (nCmdPos < sCmdLine.GetLength() &&
  122.         (sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
  123.         nCmdPos++;
  124.  
  125.     nWordPos = nCmdPos;
  126.  
  127.     // extract ini file path
  128.     while (nCmdPos < sCmdLine.GetLength() &&
  129.         sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
  130.         nCmdPos++;
  131.  
  132.     sContentFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));
  133.  
  134.     // skip LWS
  135.     while (nCmdPos < sCmdLine.GetLength() &&
  136.         (sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
  137.         nCmdPos++;
  138.  
  139.     nWordPos = nCmdPos;
  140.  
  141.     // extract ini file path
  142.     while (nCmdPos < sCmdLine.GetLength() &&
  143.         sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
  144.         nCmdPos++;
  145.  
  146.     sOutputFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));
  147. }
  148.  
  149. void CRegApp::ParseIniFile(CString sIniFile)
  150. {
  151.     int nIniPos = 0;
  152.     int nBufLen;
  153.     CString sLine;
  154.     char lpszBuf[2048];
  155.  
  156.     nBufLen = GetPrivateProfileSection("Form Literal",(LPSTR)lpszBuf,2048,sIniFile.GetBuffer(0));
  157.  
  158.     do
  159.     {
  160.         sLine = "";
  161.  
  162.         while (nIniPos < nBufLen &&    lpszBuf[nIniPos] != '\0')
  163.         {
  164.             sLine += lpszBuf[nIniPos];
  165.             nIniPos++;
  166.         }
  167.         nIniPos++; // let skip null char
  168.             
  169.         // if line is not empty, remember it
  170.         if (sLine != "")
  171.         {
  172.             m_saParams.Add(sLine);
  173.         }
  174.  
  175.     } while (sLine != "");
  176.  
  177. }
  178.  
  179. void CRegApp::CreateResponse(CString sOutputFile, CString sIniFile)
  180. {
  181.     int i;
  182.     int nIndex;
  183.     CString sResponse;
  184.     CFile fOutput;
  185.     CFileStatus fStatus;
  186.     CString sCustomer;
  187.     CString sKey;
  188.     CString sVal;
  189.  
  190.     // create header and background
  191.     sResponse = "Content-type: text/html\r\n";
  192.     sResponse += "\r\n";
  193.     sResponse += "<HTML>\r\n";
  194.     sResponse += "<BODY>\r\n";
  195.     sResponse += "<HEAD>\r\n";
  196.     sResponse += "<TITLE>ILAR CGI sample</TITLE>\r\n";
  197.     sResponse += "<BODY BACKGROUND=\"/images/back.gif\">\r\n";
  198.     sResponse += "</HEAD>\r\n";
  199.  
  200.     // do we have all params
  201.     for (i = 0; i < m_saParams.GetSize(); i++)
  202.     {
  203.         sKey.Empty(); // just in case
  204.         sVal.Empty();
  205.  
  206.         if ((nIndex = m_saParams[i].Find('=')) != -1)
  207.         {
  208.             sKey = m_saParams[i].Left(nIndex);
  209.             sVal = m_saParams[i].Right(m_saParams[i].GetLength() - nIndex - 1);
  210.         }
  211.  
  212.         if (sVal == "")
  213.         {
  214.             sResponse += "<H1>Sorry, you did not enter all fields</H1>\r\n";
  215.             break;
  216.         }
  217.  
  218.         sCustomer += "<H3>";
  219.         sCustomer += sKey;
  220.         sCustomer += ": ";
  221.         sCustomer += sVal;
  222.         sCustomer += "</H3>\r\n";
  223.     }
  224.  
  225.     // yes we have all params
  226.     if (i == m_saParams.GetSize())
  227.     {
  228.         sResponse += "<H1>Registration confirmed:</H1>\r\n";
  229.         sResponse += sCustomer;
  230.     }
  231.  
  232.     sResponse += "</HTML>\r\n";
  233.     sResponse += "</BODY>\r\n";
  234.  
  235.     // save our response into output file; name provided by WWW Server
  236.     if (fOutput.Open(sOutputFile.GetBuffer(0),
  237.         CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
  238.     {
  239.         fOutput.Write(sResponse.GetBuffer(0),sResponse.GetLength());
  240.         fOutput.Close();
  241.     }
  242. }